<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/20
 * Time: 16:46
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Member extends Base
{
    /**
     * 用户列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $where['user_status'] = ['neq', 3];
            if(isset($keyword) && $keyword != ''){
                $where['user_id|user_username'] = ['like', '%' . $keyword . '%'];
            }
            if(isset($start) && $start != "" && isset($end) && $end == "")
            {
                $where['user_time'] = ['>=',$start];
            }
            if(isset($end) && $end != "" && isset($start) && $start == "")
            {
                $where['user_time'] = ['<=',$end];
            }
            if(isset($start) && $start != "" && isset($end) && $end != "")
            {
                $where['user_time'] = ['between',[$start,$end]];
            }
            
            if(isset($user_auth) && $user_auth != "")
            {
                $where['user_auth'] = $user_auth;
            }
            if(isset($user_live) && $user_live != "")
            {
                $where['user_live'] = $user_live;
            }
            if(isset($user_status) && $user_status != "")
            {
                $where['user_status'] = $user_status;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('u_user')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('u_user')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('user_status asc,user_time desc')
                ->select();
            if($lists){
                foreach ($lists as $k => $v){
                    $lists[$k]['signname'] = '无';
                    if($v['user_sign']){
                        $name_arr = Db::table('up_sign')->where('sign_id','in',$v['user_sign'])->column('sign_name');
                        $lists[$k]['signname'] = implode($name_arr,',');
                    }
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 冻结
     * @return \think\response\Json
     */
    public function down(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 启用
     * @return \think\response\Json
     */
    public function up(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 设置
     * @return mixed
     */
    public function memberDetail(){
        if(Request::instance()->isPost()){
            $user_id = input('user_id');
            $user_username = trim(input('user_username'), ' ');
            $user_image = input('user_image');
            $user_auth = input('user_auth');
            $data = [
                'user_username' => $user_username,
                'user_image' => $user_image,
                'user_auth' => $user_auth,
                'user_updatetime' => date('Y-m-d H:i:s')
            ];

            $res = Db::table('up_u_user')->where('user_id', $user_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $user_id = input('user_id');
        $info = Db::name('u_user')
            ->where('user_id', $user_id)
            ->find();
        $this->assign('info', $info);
        return $this->fetch('member/memberdetail');
    }
    //-----------------------------------------------------------------------------
    //-----------------------------------------------------------------------------
    /**
     * 专家列表
     * @return mixed|\think\response\Json
     */
    public function special(){
        if(request()->isAjax()){
            extract(input());
            $where['user_status'] = ['neq', 3];
            $where['user_auth'] = ['>', 0];
            if(isset($keyword) && $keyword != ''){
                $where['user_id|user_username'] = ['like', '%' . $keyword . '%'];
            }
            if(isset($start) && $start != "" && isset($end) && $end == "")
            {
                $where['user_time'] = ['>=',$start];
            }
            if(isset($end) && $end != "" && isset($start) && $start == "")
            {
                $where['user_time'] = ['<=',$end];
            }
            if(isset($start) && $start != "" && isset($end) && $end != "")
            {
                $where['user_time'] = ['between',[$start,$end]];
            }

            if(isset($user_auth) && $user_auth != "")
            {
                $where['user_auth'] = $user_auth;
            }
            if(isset($user_live) && $user_live != "")
            {
                $where['user_live'] = $user_live;
            }
            if(isset($user_status) && $user_status != "")
            {
                $where['user_status'] = $user_status;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('u_user')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('u_user')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('user_status asc,user_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    /**
     * 编辑
     * @return mixed
     */
    public function editSet(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'user_username' => $user_username,
                'user_image' => $user_image,
                'user_auth' => $user_auth,
                'user_title' => $user_title,
                'user_tags' => $user_tags,
                'user_repay' => $user_repay,
                'user_hot' => $user_hot,
                'user_live' => $user_live,
                'user_updatetime' => date('Y-m-d H:i:s')
            ];

            $res = Db::table('up_u_user')->where('user_id', $user_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $user_id = input('user_id');
        $info = Db::name('u_user')
            ->where('user_id', $user_id)
            ->find();
        $this->assign('info', $info);
        return $this->fetch('member/special_edit');
    }
    //------15天数据---------------------------------------------------------------
    /**
     * 15天数据
     * @return mixed|\think\response\Json
     */
    public function userDate(){
        if(request()->isAjax()){
            extract(input());
            $where = [
                'ud_user_id' => session('user_id')
            ];
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('user_date')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('user_date')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('ud_order desc')
                ->select();
            if($lists){
                foreach ($lists as $k => $v){
                    $arr = json_decode($v['ud_json'],true);
                    $str = str_replace(['{', '"', '}'], ['', '', ''], json_encode($arr, JSON_UNESCAPED_UNICODE));
                    $lists[$k]['ud_json'] = $str;
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $user_id = input('user_id');
        session('user_id', $user_id);
        return $this->fetch('member/special_date');
    }
    /**
     * 新增数据
     * @return mixed
     */
    public function addDate(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            $data = [];
            for($i=$start;$i<$end;$i++){
                $data[$i] = input('date_'.$i);
            }
            $dateJson = json_encode($data);
            $insert = [
                'ud_user_id' => session('user_id'),
                'ud_order' => $start,
                'ud_json' => $dateJson,
                'ud_time' => $today,
                'ud_update' => $today,
            ];
            $res = Db::table('up_user_date')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $info = Db::table('up_user_date')
            ->where('ud_user_id',session('user_id'))
            ->order('ud_order desc')
            ->find();
        if($info){
            $start = $info['ud_order'] + 15;
            $end = $info['ud_order'] + 30;
        }else{
            $start = 1;
            $end = 16;
        }
        $this->assign('start', $start);
        $this->assign('end', $end);
        return $this->fetch('member/date_add');
    }
    /**
     * 编辑数据
     * @return mixed
     */
    public function editDate(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            $data = [];
            for($i=$start;$i<$end;$i++){
                $data[$i] = input('date_'.$i);
            }
            $dateJson = json_encode($data);
            $update = [
                'ud_json' => $dateJson,
                'ud_update' => $today,
            ];
            $res = Db::table('up_user_date')->where('ud_id',input('ud_id'))->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $ud_id = input('ud_id');
        $info = Db::table('up_user_date')
            ->where('ud_id',$ud_id)
            ->find();
        $info['ud_json'] = json_decode($info['ud_json'],true);
        $start = $info['ud_order'];
        $end = $info['ud_order'] + 15;
        $this->assign('start', $start);
        $this->assign('end', $end);
        $this->assign('info', $info);
        return $this->fetch('member/date_edit');
    }
    /**
     * 删除数据
     * @return \think\response\Json
     */
    public function delDate(){
        $ud_id = input('id');
        $res = Db::table('up_user_date')->where('ud_id', $ud_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    //------历史方案---------------------------------------------------------------
    /**
     * 历史方案
     * @return mixed|\think\response\Json
     */
    public function userPlan(){
        if(request()->isAjax()){
            extract(input());
            $where = [
                'up_user_id' => session('user_id')
            ];
            if(isset($keyword) && $keyword != ''){
                $where['up_title'] = ['like', '%' . $keyword . '%'];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('user_plan')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('user_plan')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('up_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $user_id = input('user_id');
        session('user_id', $user_id);
        return $this->fetch('member/special_plan');
    }
    /**
     * 新增方案
     * @return mixed
     */
    public function addPlan(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            $insert = [
                'up_user_id' => session('user_id'),
                'up_title' => $up_title,
                'up_match_time' => $up_match_time,
                'up_league' => $up_league,
                'up_home' => $up_home,
                'up_away' => $up_away,
                'up_type' => $up_type,
                'up_color' => $up_color,
                'up_time' => $today,
            ];
            $res = Db::table('up_user_plan')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        return $this->fetch('member/plan_add');
    }
    /**
     * 编辑方案
     * @return mixed
     */
    public function editPlan(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            $update = [
                'up_title' => $up_title,
                'up_match_time' => $up_match_time,
                'up_league' => $up_league,
                'up_home' => $up_home,
                'up_away' => $up_away,
                'up_type' => $up_type,
                'up_color' => $up_color,
                'up_time' => $today,
            ];
            $res = Db::table('up_user_plan')->where('up_id', $up_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $up_id = input('up_id');
        $info = Db::table('up_user_plan')
            ->where('up_id',$up_id)
            ->find();
        $this->assign('info', $info);
        return $this->fetch('member/plan_edit');
    }
    /**
     * 删除方案
     * @return \think\response\Json
     */
    public function delPlan(){
        $up_id = input('id');
        $res = Db::table('up_user_plan')->where('up_id', $up_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}